define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'blog/articles/index' + location.search,
                    add_url: 'blog/articles/add',
                    edit_url: 'blog/articles/edit',
                    del_url: 'blog/articles/del',
                    multi_url: 'blog/articles/multi',
                    table: 'blog_articles',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                escape: false,
                sortName: 'weigh',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user_id', title: __('User_id') },
                        { field: 'article_title', title: __('Article_title') },
                        { field: 'article_image', title: __('Article_image'), events: Table.api.events.image, formatter: Table.api.formatter.image },
                        // { field: 'article_content', title: __('Article_content'), cellStyle: function() { return { css: { "width": "100px" } } } },
                        { field: 'article_views', title: __('Article_views') },
                        { field: 'totalcomments', title: __('Article_comment_count') },
                        { field: 'article_date', title: __('Article_date'), operate: 'RANGE', addclass: 'datetimerange' },
                        { field: 'article_like_count', title: __('Article_like_count') },
                        { field: 'sorts_ids', title: __('Sorts_ids') },
                        { field: 'labels_ids', title: __('Labels_ids') },
                        // { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'weigh', title: __('Weigh') },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.toggle },
                        { field: 'blogsorts.sort_name', title: __('Blogsorts.sort_name') },
                        { field: 'bloglabels.label_name', title: __('Bloglabels.label_name') },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });
            // 绑定TAB事件
            $(document).on('click', ".btn-clean", function() {
                var url = $(this).data("url");
                layer.confirm('确定删除所有文章索引呢？', {
                    btn: ['是的', '取消'] //按钮
                }, function() {
                    Fast.api.ajax({
                        url: url,
                        data: '',
                    }, function(data, ret) {
                        Layer.alert("执行结果：" + JSON.stringify(data));

                    }, function() {
                        window.parent.$(".toolbar .btn-refresh").trigger('click');
                    });
                });

            });
            $(document).on('click', ".btn-rebuild", function() {
                var url = $(this).data("url");
                layer.confirm('确定重建所有文章索引？', {
                    btn: ['是的', '取消'] //按钮
                }, function() {
                    Fast.api.ajax({
                        url: url,
                        data: '',
                    }, function(data, ret) {
                        Layer.alert("执行结果：" + JSON.stringify(data));

                    }, function() {
                        window.parent.$(".toolbar .btn-refresh").trigger('click');
                    });
                });

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});